
package com.rasenhund.android.paintstock;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.SeekBar;
import android.widget.TextView;

import com.rasenhund.android.paintstock.schema.Stock;

public class StockListAdapter extends ArrayAdapter<Stock> {

    private ArrayList<Stock> mItems;
    private ArrayList<Stock> mAllItems;
    private LayoutInflater mInflater;
    private int mTextViewResourceId;
    private StockFilter mFilter = null; // 独自Filterクラス

    public StockListAdapter(Context context, int textViewResourceId, ArrayList<Stock> objects) {
        super(context, textViewResourceId, objects);

        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mTextViewResourceId = textViewResourceId;
        mItems = objects;
        mAllItems = (ArrayList<Stock>) objects.clone();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view;

        if (convertView != null) {
            view = convertView;
        } else {
            view = mInflater.inflate(mTextViewResourceId, null);
        }

        Stock item = (Stock) getItem(position);

        if (item != null) {

            // @@@ TODO 在庫あり、在庫なしでIdを共通化
            // 存在チェックも行うようにして複数のViewで同じ動きをするように

            TextView txtName = (TextView) view.findViewById(R.id.txtName);
            txtName.setText(item.getPaintName());

            SeekBar seekAmount = (SeekBar) view.findViewById(R.id.seekAmount);
            seekAmount.setProgress(item.getAmount());
            seekAmount.setEnabled(false);

            TextView txtStock = (TextView) view.findViewById(R.id.txtStock);
            txtStock.setText(String.valueOf(item.getStock()) + "本");

            // すべてリスト用、本数、残量共に0(=在庫なし)の場合、非表示
            if (item.getStock() == 0 && item.getAmount() == 0) {
                seekAmount.setVisibility(View.INVISIBLE);
                txtStock.setVisibility(View.INVISIBLE);
            } else {
                seekAmount.setVisibility(View.VISIBLE);
                txtStock.setVisibility(View.VISIBLE);
            }

        }

        return view;

    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Stock getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mItems.get(position).get_id();
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new StockFilter();
        }
        return mFilter;
    }

    /**
     * 特定Itemのセット(更新後の再表示用)
     * 
     * @param position
     * @param stock
     */
    public void setItem(int position, Stock stock) {
        mItems.set(position, stock);
    }

    /**
     * フィルタ処理
     */
    private class StockFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            constraint = constraint.toString().toLowerCase();
            FilterResults results = new FilterResults();

            if (constraint != null && constraint.toString().length() > 0) {
                ArrayList<Stock> filteredItems = new ArrayList<Stock>();
                for (int i = 0, l = mAllItems.size(); i < l; i++) {
                    Stock item = mAllItems.get(i);
                    // 塗料名称の部分一致とする
                    if (item.getPaintName().toLowerCase().contains(constraint)) {
                        filteredItems.add(item);
                    }
                    results.values = filteredItems;
                    results.count = filteredItems.size();
                }
            } else {
                synchronized (this) {
                    results.values = mAllItems;
                    results.count = mAllItems.size();
                }
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            ArrayList<Stock> filteredItems = (ArrayList<Stock>) results.values;

            if (filteredItems != null && filteredItems.size() > 0) {
                notifyDataSetChanged();
                clear();
                for (int i = 0, l = filteredItems.size(); i < l; i++) {
                    add(filteredItems.get(i));
                }
                notifyDataSetInvalidated();
            }
        }

    }

}
